/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.refer.ref;

import org.zhiqim.git.objer.objectid.ObjectId;
import org.zhiqim.git.refer.Ref;


/**
 * 对象引用，引用指向一个真实对象
 * @see SymbolicRef 快捷方式引用，引用指向目标引用
 *
 * @version v1.0.0 @author zhichenggang 2017-3-23 新建与整理
 */
public class ObjectIdRef implements Ref
{
    private final String name;
    
    //关联对象编号
    private final ObjectId objectId;
    private final boolean stored;
    
    //剥除之后的对象
    private boolean isPeeled;
    private ObjectId peeledObjectId;
    
    public ObjectIdRef(String name)
    {
        this.name = name;
        this.objectId = null;
        this.stored = false;
    }
    
    public ObjectIdRef(String name, ObjectId id)
    {
        this.name = name;
        this.objectId = id;
        this.stored = id != null;
    }
    
    public ObjectIdRef(ObjectIdRef ref)
    {
        this.name = ref.getName();
        this.objectId = ref.getObjectId();
        this.stored = ref.isStored();
    }
    
    public String getName()
    {
        return name;
    }

    /**********************************************************************************/
    //快捷方式
    /**********************************************************************************/
    
    public boolean isSymbolic()
    {
        return false;
    }

    public Ref getTarget()
    {
        return this;
    }
    
    public ObjectIdRef getUltimateTarget()
    {
        return this;
    }
    
    /**********************************************************************************/
    //标签判断
    /**********************************************************************************/
    
    public ObjectId getPeeledObjectId()
    {
        return peeledObjectId;
    }

    public boolean isPeeled()
    {
        return isPeeled;
    }

    public void setPeeled()
    {
        isPeeled = true;
    }
    
    public void setPeeledObjectId(ObjectId peeledObjectId)
    {
        this.peeledObjectId = peeledObjectId;
    }
    
    /**********************************************************************************/
    //对象&文件
    /**********************************************************************************/

    public ObjectId getObjectId()
    {
        return objectId;
    }

    public boolean isStored()
    {
        return stored;
    }

    /**********************************************************************************/
    //toString
    /**********************************************************************************/
    
    @Override
    public String toString()
    {
        return new StringBuilder("Ref")
            .append("[").append(getName())
            .append('=').append(ObjectId.toString(objectId))
            .append(']')
            .toString();
    }
}
